package com.imooc.sellspringcloud.server.conver;


import com.google.gson.Gson;
import com.google.gson.JsonSyntaxException;
import com.google.gson.reflect.TypeToken;
import com.imooc.sellspringcloud.server.dataobject.OrderDetail;
import com.imooc.sellspringcloud.server.dto.OrderDTO;
import com.imooc.sellspringcloud.server.enums.OrderExceptionEnum;
import com.imooc.sellspringcloud.server.exception.OrderException;
import com.imooc.sellspringcloud.server.form.OrderForm;
import lombok.extern.slf4j.Slf4j;

import java.util.List;

/**
 * @author
 * @create 2019-07-27 22:10
 **/
@Slf4j
public class OrderConver {

    public static OrderDTO orderForm2OrderDTO(OrderForm orderForm) {

        OrderDTO orderDTO = new OrderDTO();
        orderDTO.setBuyerName(orderForm.getName());
        orderDTO.setBuyerPhone(orderForm.getPhone());
        orderDTO.setBuyerAddress(orderForm.getAddress());
        orderDTO.setBuyerOpenid(orderForm.getOpenid());
        Gson gson = new Gson();
        try {
            List<OrderDetail> orderDetails = gson.fromJson(orderForm.getItems(),
                    new TypeToken<List<OrderDetail>>() {
                    }.getType());
            orderDTO.setOrderDetailList(orderDetails);
        } catch (JsonSyntaxException e) {
            log.error("orderForm转换错误 {}", e.getMessage());
            throw new OrderException(OrderExceptionEnum.ORDER_CONVER.getCode(), e.getMessage());
        }
        return orderDTO;
    }
}